#! /bin/sh
# PCP QA Test No. 170
# Test $PCP_RC_DIR/pmcd script pmcd log location
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

. ./common.product
. ./common.check
. ./common.filter

status=0

_cleanup()
{
    _restore_config $PCP_PMCDOPTIONS_PATH
    _service pmcd restart 2>&1 | _filter_pcp_restart
    _wait_for_pmcd
    _restore_auto_restart pmcd
    _service pmlogger restart 2>&1 | _filter_pcp_restart
    _wait_for_pmlogger
    $sudo rm -f $tmp.*
    exit $status
}

trap _cleanup 0 1 2 3 15

_stop_auto_restart pmcd

# real QA test starts here

echo "First, the standard logfile:" | tee -a $seq_full
if ! _service pmlogger stop 2>&1; then _exit 1; fi \
| _filter_pcp_stop >>$seq_full
_wait_pmlogger_end || _exit 1
if ! _service pmcd stop 2>&1; then _exit 1; fi \
| _filter_pcp_stop >>$seq_full
_wait_pmcd_end || _exit 1
$sudo rm -f $PCP_LOG_DIR/pmcd/pmcd.log*
if ! _service pmcd start 2>&1; then _exit 1; fi \
| tee -a $seq_full >$tmp.out
_wait_for_pmcd || _exit 1
if ! _service pmlogger start 2>&1; then _exit 1; fi \
| tee -a $seq_full >>$tmp.out
_wait_for_pmlogger || _exit 1
grep -i 'starting pmcd' $tmp.out | _filter_pcp_start
if [ -f $PCP_LOG_DIR/pmcd/pmcd.log ]
then
    echo '$PCP_LOG_DIR/pmcd/pmcd.log created'
else
    echo 'Error: $PCP_LOG_DIR/pmcd/pmcd.log not created' | tee -a $seq_full
    date >>$seq_full
    ls -l $PCP_LOG_DIR/pmcd >>$seq_full
    $PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep -E '[P]PID|/[p]mcd( |$)' >>$seq_full

    status=1
fi
echo ""

_save_config $PCP_PMCDOPTIONS_PATH
cp $PCP_PMCDOPTIONS_PATH $tmp.options
cat <<End-Of-File >>$tmp.options
# Dummy lines added for PCP QA test $seq
#
-l $tmp.1.pmcd.log
-l $tmp.2.pmcd.log
End-Of-File
$sudo cp $tmp.options $PCP_PMCDOPTIONS_PATH

echo "The logfile from the hacked \$PCP_PMCDOPTIONS_PATH (pmcd.options):" | tee -a $seq_full
if ! _service pmcd restart 2>&1; then _exit 1; fi \
| tee -a $seq_full >$tmp.out
_wait_for_pmcd || _exit 1
if ! _service pmlogger restart 2>&1; then _exit 1; fi \
| tee -a $seq_full >>$tmp.out
_wait_for_pmlogger || _exit 1
grep -i 'starting pmcd' $tmp.out | sed -e "s/$$/MYPID/" | _filter_pcp_start
if [ -f $tmp.1.pmcd.log ]
then
    echo 'Error: $tmp.1.pmcd.log created unexpectedly'
    ls -l $tmp.1.pmcd.log
    cat $tmp.1.pmcd.log
    status=1
fi
if [ -f $tmp.2.pmcd.log ]
then
    echo '$tmp.2.pmcd.log created'
else
    echo 'Error: $tmp.2.pmcd.log not created'
    status=1
fi

exit
